package com.wonder.taxi.api;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.geojson.GeoJsonObject;
import org.geojson.LineString;
import org.geojson.LngLatAlt;
import org.geojson.Point;

import com.wonder.gis.model.GeometryType;
import com.wonder.taxi.base.BaseService;

public class GISBaseService extends BaseService {
	public static void closeResultSetResource(ResultSet rs)
	{
		Statement stmt = null;
		Connection conn = null;
		try {
			try {
				stmt = rs.getStatement();
				conn = stmt.getConnection();
			} finally {
				rs.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		} catch (NullPointerException e) {
		}

	}

	public static void closeStatementResource(Statement st)
	{
		Connection conn = null;
		try {
			try {
				conn = st.getConnection();
			} finally {
				st.close();
				conn.close();
			}
		} catch (SQLException e) {
		}
	}

	public GeoJsonObject getGeometry(GeometryType type, ResultSet rs)
	{
		double scale = 10000000;
		try {
			switch (type) {
			case POINT:
				Point point = null;
				if (rs.next())
					point = new Point(rs.getInt("longitude") / scale,
							rs.getInt("latitude") / scale);
				return point;
			case LINE_STRING:
				LineString line_string = new LineString();
				while (rs.next()) {
					line_string.add(new LngLatAlt(rs.getInt("longitude")
							/ scale, rs.getInt("latitude") / scale));
				}
				return line_string;
			case MULTI_LINE_STRING:
				break;
			case MULTI_POINT:
				break;
			case MULTI_POLYGON:
				break;
			case POLYGON:
				break;
			default:
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}